﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using MySql.Data.MySqlClient;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TJ_MainNameSpace
{
    public partial class Form_35KV_ShouChe : Form
    {
        public Form_35KV_ShouChe()
        {
            InitializeComponent();
        }
        private kongjian51.Shouche35kv_Up _myshoucheU = null;
        private kongjian83.Shouche35kv_Down _myshoucheD = null;
        private bool loading = false;
        private bool _Up = false;
        private ZTName _ctr_kind = ZTName.ZTNameCount;

        MySqlConnection con = new MySqlConnection(TongJi_EMS.Properties.Settings.Default.monitorConnection);

        public Form_35KV_ShouChe(kongjian51.Shouche35kv_Up id)
        {
            InitializeComponent();
            this._myshoucheU = id;
            _Up = true;
        }

        public Form_35KV_ShouChe(kongjian83.Shouche35kv_Down id)
        {
            InitializeComponent();
            this._myshoucheD = id;
            _Up = false;
        }

        private void Form_51_Load(object sender, EventArgs e)
        {
            loading = true;
            if (_Up)
            {
                this.textBox1.Text = _myshoucheU.Site.Name;
                this.textBox2.Text = _myshoucheU.Bol;
                this.textBox3.Text = _myshoucheU.FaultSignal;
            }
            else
            {
                this.textBox1.Text = _myshoucheD.Site.Name;
                this.textBox2.Text = _myshoucheD.Bol;
                this.textBox3.Text = _myshoucheD.FaultSignal;
            }
            try
            {
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }
                string sql1 = "select id, description from dbarea where project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'";

                MySqlDataAdapter oda1 = new MySqlDataAdapter(sql1, con);

                DataSet ds3 = new DataSet();

                oda1.Fill(ds3, "dbarea");

                this.comboBox1.DataSource = ds3.Tables[0];
                this.comboBox1.DisplayMember = "description";
                this.comboBox1.ValueMember = "id";
                con.Close();
            }
            catch (Exception e1)
            {
                MessageBox.Show(e1.Message);
            }
            loading = false;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (_Up)
            {
                _myshoucheU.DbAreaID = (int)comboBox1.SelectedValue;
                _myshoucheU.DbpointID = (int)comboBox2.SelectedValue;
                _ctr_kind = ZTName.kj51;
            }
            else
            {
                _myshoucheD.DbAreaID = (int)comboBox1.SelectedValue;
                _myshoucheD.DbpointID = (int)comboBox2.SelectedValue;
                _ctr_kind = ZTName.kj83;
            }
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            try
            {
                string mysql = "SELECT  ctr_name  FROM  run_ctr where ctr_name='"
                    + textBox1.Text + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName
                    + "' AND subpage='" + TongJi_EMS._Management.ProjectManager.Instance.CurSubPage + "'";
                //string mysql = "SELECT   COUNT( txt_name='" + mytext.Name + "') FROM  run_temp ";

                MySqlCommand cmd = new MySqlCommand(mysql, con);
                object count = cmd.ExecuteScalar();

                string sql;

                if (count == null)

                    sql = "INSERT INTO run_ctr(ctr_name,ctr_kind,project,db_pointid,subpage) VALUES('"
                        + textBox1.Text + "' ," + Convert.ToInt32(_ctr_kind) + ",'"
                        + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'," + (int)comboBox2.SelectedValue + ",'"
                        + TongJi_EMS._Management.ProjectManager.Instance.CurSubPage + "')";
                //sql = "INSERT INTO run_ctr(ctr_name,db_name,ctr_kind) VALUES( '" + textBox1.Text + "' ,'" + kind + "','" + kind + "')";
                else
                    sql = "update run_ctr set db_pointid='" + (int)comboBox2.SelectedValue + "' where ctr_name='"
                        + textBox1.Text + "'AND project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'AND subpage='"
                        + TongJi_EMS._Management.ProjectManager.Instance.CurSubPage + "'";

                MySqlCommand mInsertCmd = new MySqlCommand(sql, con);
                mInsertCmd.CommandType = CommandType.Text;
                mInsertCmd.ExecuteNonQuery();
                con.Close();
                //myshouche.Name = this.textBox1.Text;
                //myshouche.Bol = this.textBox2.Text;
                //myshouche.FaultSignal = this.textBox3.Text;
                //this.myshouche.Refresh();
                this.Close();
            }
            catch (Exception exp)
            {
                MessageBox.Show(exp.ToString());
            }

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (loading) return;
            fresh();
        }

        private void fresh()
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            string sql = "select id, db_description from dbpoint where project='" + TongJi_EMS._Management.ProjectManager.Instance.CurProjectName + "'AND db_areaid='" + (int)comboBox1.SelectedValue + "'";
            MySqlDataAdapter oda = new MySqlDataAdapter(sql, con);

            DataSet ds = new DataSet();
            ds.Clear();
            oda.Fill(ds, "dbpoint");
            this.comboBox2.DataSource = ds.Tables["dbpoint"];
            this.comboBox2.DisplayMember = "db_description";
            this.comboBox2.ValueMember = "id";
            this.comboBox3.DataSource = ds.Tables["dbpoint"];
            this.comboBox3.DisplayMember = "db_description";
            this.comboBox3.ValueMember = "id";
            con.Close();
        }
    }
}